package com.learn.cloud.validator.config;

import lombok.Data;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

/**
 * @description: 自定义UserValidator
 * <p>https://blog.csdn.net/weixin_47952625/article/details/110874173</p>
 * @author: cong zhi
 * @createDate: 2022/2/6 12:52
 * @updateUser: cong zhi
 * @updateDate: 2022/2/6 12:52
 * @updateRemark: 修改内容
 * @version: v1.0
 */
@Configuration
public class UserValidator implements Validator {
    @Override
    public boolean supports(Class<?> clazz) {
        return clazz.equals(UserLogin.class);
    }

    @Override
    public void validate(Object target, Errors errors) {
        ValidationUtils.rejectIfEmpty(errors, "userName", "userName.empty");
        ValidationUtils.rejectIfEmpty(errors, "password", "password.empty");
        UserLogin userLogin = (UserLogin) target;
        System.out.println(userLogin);
    }

    @Data
    public class UserLogin {
        private String userName;

        private String password;
    }
}
